﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shl01.Models
{
    public class SeModex
    {
        public string TypeName { get; set; }
        public Dictionary<string, string> dicType { get; set; }
        public string PrimaryName { get; set; }
        public string DBName { get; set; }
        public string Id { get; set; }
        public string GetStr()
        {
            string paras = "";
            string Column = "";
            string ColumnValue = "";
            string UpdateColumn = "";
            string Params1 = "";//修改
            string Params2 = "";//添加
            string Params3 = "";//删除
            foreach (KeyValuePair<string, string> key in dicType)
            {
                Params1 = Params1 + string.Format("new SqlParameter('@{0}',g.{0}),", key.Key);
                switch (key.Value)
                {
                    case "int":
                        paras += string.Format(" {0} = Convert.ToInt32(reader['{0}']), ", key.Key);
                        break;
                    case "string":
                        paras += string.Format(" {0} = Convert.ToString(reader['{0}']), ", key.Key);
                        break;
                    case "DateTime":
                        paras += string.Format(" {0} = Convert.ToDateTime(reader['{0}']), ", key.Key);
                        break;
                    case "bool":
                        paras += string.Format(" {0} = Convert.ToBoolean(reader['{0}']), ", key.Key);
                        break;
                    case "double":
                        paras += string.Format(" {0} = Convert.ToDouble(reader['{0}']), ", key.Key);
                        break;
                }
                if (PrimaryName != key.Key)
                {
                    Params2 = Params2 + string.Format("new SqlParameter('@{0}',g.{0}),", key.Key);
                    Column += string.Format("{0},", key.Key);
                    ColumnValue += string.Format("@{0},", key.Key);
                    UpdateColumn += string.Format("{0} = @{0},", key.Key);
                }
            }
            Params3 = Params3 + string.Format("new SqlParameter('@{0}',g.{0}),", PrimaryName);
            string zhuanHuan = string.Format("{0} g = new {0}(){2} {1}{3};", TypeName, paras.Substring(0, paras.Length - 2), "{", "}");
            string insertStr = string.Format("insert into {2}({0}) values({1}))", Column.Substring(0, Column.Length - 1), ColumnValue.Substring(0, ColumnValue.Length - 1), TypeName);
            string UpdateStr = string.Format("update {2} set {0} where {1} = @{1}", UpdateColumn, PrimaryName, TypeName);

            string str = @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using " + DBName + @".Models;
using System.Data.SqlClient;
using System.Data;

namespace " + DBName + @".Dal
{
    [Serializable]
    public static class GradeService
    {
        /// <summary>
        /// 根据条件查询多个，返回List集合
        /// </summary>
        /// <param name='where'>条件</param>
        /// <param name='paras'>参数</param>
        /// <returns>List集合</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
            public static List<Grade> GetGradeReader(string where = null,SqlParameter[] paras = null)
        {
            List<Grade> list = new List<Grade>();
            string sql = 'select * from Grade where 1=1 ';
            if (where != null)
            {
                sql += 'and ' + where;
            }
            try
            { 
                SqlDataReader reader = SQLHelper.ExecuteReader(sql,paras);
                while (reader.Read())
                {
                    " + zhuanHuan + @"
                    list.Add(g);
                }
                reader.Close();
            }
            catch
            {

                throw;
            }
            return list;
        }
         /// <summary>
        /// 根据条件查询多个，返回DataSet集合
        /// </summary>
        /// <param name='where'>条件</param>
        /// <param name='paras'>参数</param>
        /// <returns>DataSet集合</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
            public static DataSet GetAllDataSet(string where = null, SqlParameter[] paras = null)
        {
            DataSet ds = null;
            string sql = 'select * from Grade where 1 = 1 ';
            if (where != null)
            {
                sql += 'and ' + where;
            }
            try
            {
                ds = SQLHelper.ExecuteTable(sql, paras);
            }
            catch
            {

                throw;
            }
            return ds;
        }
        
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name='g'>要添加的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
        public static int Add(Grade g)
        {
            string sql = @'" + insertStr + @"';
            SqlParameter[] paras =
            {
                " + Params2.Substring(0, Params2.Length - 1) + @"
            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql,paras);
            }
            catch
            {

                throw;
            }
        }
         /// <summary>
        /// 修改
        /// </summary>
        /// <param name='g'>要修改后的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
        public static int Update(Grade g)
        {
            string sql = @'" + UpdateStr.Substring(0, UpdateStr.Length - 1) + @"';
            SqlParameter[] paras =
            {
                " + Params1.Substring(0, Params1.Length - 1) + @"
            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql,paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据对象删除数据
        /// </summary>
        /// <param name='g'>要删除的对象</param>
        /// <returns>受影响行数</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
        public static int Delete(Grade g)
        {
            string sql = 'delete Grade where " + string.Format("{0} = @{0}", PrimaryName) + @"';
            SqlParameter[] paras =
            {
                " + Params3.Substring(0, Params3.Length - 1) + @"
            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql,paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <param name='gradeid'>主键</param>
        /// <returns>受影响行数</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
        public static int Delete(int gradeid)
        {
            string sql = 'delete Grade where " + string.Format("{0} = @{0}", PrimaryName) + @"';
            SqlParameter[] paras =
            {
                new SqlParameter(" + string.Format("'@{0}',{1}", PrimaryName, "gradeid") + @")
            };
            try
            {
                return SQLHelper.ExecuteNonQuery(sql,paras);
            }
            catch
            {

                throw;
            }
        }
        /// <summary>
        /// 根据条件删除
        /// </summary>
        /// <param name='where'>条件</param>
        /// <param name='paras'>参数</param>
        /// <returns>受影响行数</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
        public static int Delete(string where = null, SqlParameter[] paras = null)
        {
            string sql = 'delete from Grade where 1 = 1 ';
            if (!string.IsNullOrEmpty(where))
            {
                sql += 'and ' + where;
            }
            try
            {
                return SQLHelper.ExecuteNonQuery(sql, paras);
            }
            catch
            {
                throw;
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name='count'>总页数</param>
        /// <param name='ji'>要访问的页码</param>
        /// <param name='size'>一页显示多少数据</param>
        /// <returns>要显示页的数据</returns>
        /// <exception cref='有异常抛出'>有异常抛出</exception>
            public static List<Grade> GetByFenYe(out int count int ji = 1, int size = 10)
            {
                string sql = 'select Count(*) from Grade';
                try
                {
                    int zong = Convert.ToInt32(SQLHelper.ExecuteScalar(sql));
                    count = zong % size > 0 ? zong / size + 1 : zong / size;
                    sql = string.Format('select top {0} * from Grade where studentno not in (select top ({0} * {1}) studentno from Grade order by studentno)order by studentno', size, ji - 1);
                    SqlDataReader result = SQLHelper.ExecuteReader(sql);
                    List<Grade> list = new List<Grade>();
                    while (result.Read())
                    {
                        Grade student = new Grade()
                        {
                            
                        };
                        list.Add(student);
                    }
                    return list;
                }
                catch
                {

                    throw;
                }
            }
        }
}
";
            str = str.Replace("Grade", TypeName);
            return str.Replace("'", "\"");
        }
    }
}
